from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QLabel, QLineEdit, QVBoxLayout


class FormLayoutDemo(QWidget):
    def __init__(self):
        super().__init__()

        # 创建表单布局
        form_layout = QFormLayout()

        self.addStretch()
        # 添加控件到表单布局
        form_layout.addRow("姓名:", QLineEdit())
        form_layout.addRow("年龄:", QLineEdit())
        form_layout.addRow("邮箱:", QLineEdit())
        self.addStretch()
        # 创建垂直布局，将表单布局添加到其中
        main_layout = QVBoxLayout()
        # main_layout.addWidget(QWidget())  # 添加一个占位符，使表单布局位于窗口中央
        main_layout.addLayout(form_layout)

        # 设置窗口的主布局
        self.setLayout(main_layout)

        # 设置窗口标题和大小
        self.setWindowTitle("表单布局示例")
        self.resize(300, 200)


if __name__ == "__main__":
    app = QApplication([])
    demo = FormLayoutDemo()
    demo.show()
    app.exec_()
